import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import welcome from '../views/welcome.vue'
import user from '../components/user.vue'
import rote from '../components/rote.vue'
import product from '../components/product.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect:'/home/welcome',
    children:[
      {
        path:'welcome',
        component:welcome
      }
    ]
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children:[
      {
        path:'welcome',
        component:welcome
      },
      {
        path:'users',
        component:user
      },
      {
        path:'rote',
        component:rote
      },
      {
        path:'product',
        component:product
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  },
  {
    path: '/father',
 
    component: () => import('../views/father.vue')
  }
]

const router = new VueRouter({
  routes
})


// 针对于后台管理的路由守卫 
// 前台的路由守卫 不登录可以给用户几个页面访问 有些核心页面必须登录
// 不登录 只能访问login 
router.beforeEach((to,from,next)=>{
   if(to.path === '/login') {
     next() 
     return 
   }
  //  如果不去Login 页面进行判断
   const tokenstr = window.sessionStorage.getItem('token')
  //  如果你没有token还想去别的页面 在后台不可能 让你强行去login
   if(!tokenstr) return next('/login')
  //  有token可以正常访问 
   next()


})
export default router
